package com.example.wjh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wjh.entity.Player;
import com.example.wjh.entity.Result;
import com.example.wjh.mapper.PlayerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 球员
 * 后端接口
 */
@CrossOrigin
@RestController
@RequestMapping("/player")
public class PlayerController {

    @Autowired
    private PlayerMapper PlayerMapper;

    //计算球员数量
    @GetMapping("/count")
    public Result getPlayerCount() {
        long count = PlayerMapper.selectCount(null);
        return Result.success(count);
    }

    //按用户名搜索
    @GetMapping("/info/{username}")
    public Result getPlayerInfoByUsername(@PathVariable("username") String username) {
        QueryWrapper<Player> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Player player = PlayerMapper.selectOne(queryWrapper);
        if (player != null) {
            return Result.success(player);
        } else {
            return Result.error("Player not found for username: " + username);
        }
    }

    /**
     * 查询所有球员
     * @return
     */
    @GetMapping()
    public Result getPlayers(){  return Result.success(PlayerMapper.selectList(null));}

    // 添加球员
    @PostMapping()
    public Result createPlayer(@RequestBody Player player) {
        PlayerMapper.insert(player);
        return Result.success();
    }

    // 更新球员信息
    @PutMapping("/{id}")
    public Result updatePlayer(@PathVariable("id") int id, @RequestBody Player player) {
        Player existingPlayer = PlayerMapper.selectById(id);
        if (existingPlayer != null) {
            player.setId(id);
            PlayerMapper.updateById(player);
            return Result.success();
        } else {
            return Result.error("Player not found");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delPlayer(@PathVariable("id") int id) {
        PlayerMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batch")
    public Result delPlayerBatch(@RequestBody List<Long> ids) {
        PlayerMapper.deleteBatchIds(ids);
        return Result.success();
    }

    /**
     * 模糊查询
     * @param
     * @return
     */
    @GetMapping("/search")
    public Result searchPlayer(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String position
    ) {
        return Result.success(PlayerMapper.searchPlayer(name,username,position));
    }

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findByPage")
    public Result getPlayerList(@RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize) {
        Page<Player> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Player> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id"); // 根据id字段降序排序
        page.addOrder(OrderItem.desc("id")); // 添加降序排序条件
        IPage ipage = PlayerMapper.selectPage(page,null);
        return Result.success(ipage);
    }

    //司职位置人数计算
    @GetMapping("/countPlayersByPosition")
    public List<Map<String, Object>> countPlayersByPosition() {
        return PlayerMapper.countPlayersByPosition();
    }
}
